package cn.iocoder.yudao.module.system.event;

import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.module.system.dal.dataobject.useractivity.UserDailyActivityDO;
import cn.iocoder.yudao.module.system.service.useractivity.UserActivityStatsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDate;

/**
 * 用户活动事件监听器
 * 
 * @author 系统开发团队
 */
@Component
@Slf4j
public class UserActivityEventListener {

    @Resource
    private UserActivityStatsService userActivityStatsService;

    /**
     * 处理用户活动事件
     */
    @EventListener
    @Async("taskExecutor")
    public void handleUserActivityEvent(UserActivityEvent event) {
        try {
            log.debug("[handleUserActivityEvent] 处理用户活动事件: userId={}, activityType={}, detail={}", 
                     event.getUserId(), event.getActivityType(), event.getActivityDetail());

            // 记录用户日常活动
            recordUserDailyActivity(event);

            // 根据活动类型进行不同处理
            switch (event.getActivityType()) {
                case UserActivityEvent.ActivityType.LOGIN:
                    handleLoginActivity(event);
                    break;
                case UserActivityEvent.ActivityType.PAGE_VIEW:
                    handlePageViewActivity(event);
                    break;
                case UserActivityEvent.ActivityType.API_CALL:
                    handleApiCallActivity(event);
                    break;
                case UserActivityEvent.ActivityType.FEATURE_USE:
                    handleFeatureUseActivity(event);
                    break;
                default:
                    log.warn("[handleUserActivityEvent] 未知的活动类型: {}", event.getActivityType());
            }

        } catch (Exception e) {
            log.error("[handleUserActivityEvent] 处理用户活动事件失败: userId={}, activityType={}", 
                     event.getUserId(), event.getActivityType(), e);
        }
    }

    /**
     * 记录用户日常活动
     */
    private void recordUserDailyActivity(UserActivityEvent event) {
        try {
            if (event.getUserId() == null) {
                return;
            }

            // 调用服务记录用户日常活动
            userActivityStatsService.recordUserDailyActivity(
                event.getUserId(), 
                LocalDate.now()
            );

        } catch (Exception e) {
            log.error("[recordUserDailyActivity] 记录用户日常活动失败: userId={}", event.getUserId(), e);
        }
    }

    /**
     * 处理登录活动
     */
    private void handleLoginActivity(UserActivityEvent event) {
        log.debug("[handleLoginActivity] 处理登录活动: userId={}, username={}", 
                 event.getUserId(), event.getUsername());
        
        // 登录活动已经在recordUserDailyActivity中处理
        // 这里可以添加额外的登录相关逻辑，如统计登录次数等
    }

    /**
     * 处理页面访问活动
     */
    private void handlePageViewActivity(UserActivityEvent event) {
        log.debug("[handlePageViewActivity] 处理页面访问活动: userId={}, uri={}", 
                 event.getUserId(), event.getRequestUri());
        
        // 可以在这里添加页面访问统计逻辑
    }

    /**
     * 处理API调用活动
     */
    private void handleApiCallActivity(UserActivityEvent event) {
        log.debug("[handleApiCallActivity] 处理API调用活动: userId={}, uri={}, method={}", 
                 event.getUserId(), event.getRequestUri(), event.getRequestMethod());
        
        // 可以在这里添加API调用统计逻辑
    }

    /**
     * 处理功能使用活动
     */
    private void handleFeatureUseActivity(UserActivityEvent event) {
        log.debug("[handleFeatureUseActivity] 处理功能使用活动: userId={}, feature={}", 
                 event.getUserId(), event.getActivityDetail());
        
        // 可以在这里添加功能使用统计逻辑
    }
}